<template>
  <el-dialog
    :close-on-click-modal="false"
    title="总承包合同结算与支付"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="6">
          <el-form-item label="项目名称" prop="projectName">
            <!-- <el-input v-model="form.projectName" :disabled="disableForm" /> -->
            <el-input v-model="form.projectName" readonly :disabled="disableForm || optType !== 'add'" @click.native="optType === 'add' ? showListModal = true : null">
              <i slot="suffix" class="el-input__icon el-icon-circle-plus-outline" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="结算月份" prop="month">
            <el-date-picker v-model="form.month" value-format="yyyy-MM" style="width:100%" type="month" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="结算产值" prop="outValue">
            <el-input v-model="form.outValue" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="应支付款" prop="payable">
            <el-input v-model="form.payable" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="已支付款" prop="paid">
            <el-input v-model="form.paid" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="扣款" prop="deduction">
            <el-input v-model="form.deduction" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="欠款" prop="arrears">
            <el-input v-model="form.arrears" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="6">
          <el-form-item label="流程状态" prop="status">
            <el-select v-model="form.status" :disabled="disableForm" style="width: 100%;" placeholder="请选择流程状态">
              <el-option v-for="item in workflowStatus" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col> -->
      </el-row>
      <div class="btns" style="padding-bottom: 10px;">
        <el-button type="primary" @click="addRow">新增行</el-button>
        <el-button type="primary" @click="delDetailDataItem">删除行</el-button>
      </div>
      <com-table
        v-model="selectRow"
        :no-page="true"
        :columns="columns"
        highlight-current-row
        :data="detailDataList"
      >
        <template v-slot:deductionName="{row}">
          <el-input v-model="row.deductionName" />
        </template>
        <template v-slot:deductionAmount="{row}">
          <el-input v-model="row.deductionAmount" />
        </template>
        <template v-slot:remark="{row}">
          <el-input v-model="row.remark" />
        </template>
      </com-table>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
    <listSelect :show-modal.sync="showListModal" title="招标信息选择" :columns="listColumns" :api="getProjectInfoList" @getSelectedData="getSelectedRows" />

  </el-dialog>
</template>

<script>
import { addGeneralContractSettleInfo, updateGeneralContractSettleInfo, getGeneralContractSettleInfoDetail, getProjectInfoList } from '@/api/contractManage'
import { getAreaList } from '@/api/common'
import { workflowStatus } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
import listSelect from '@/components/ListSelect'
export default {
  name: 'AddGeneralContractSettleInfo',
  components: {
    fileUploader,
    listSelect
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      getProjectInfoList,
      showFileModal: false,
      showListModal: false,
      targetList: [],
      workflowStatus,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        projectName: '',
        month: '',
        outValue: '',
        payable: '',
        paid: '',
        deduction: '',
        arrears: '',
        remark: ''
      },
      showAddDetailModal: false,
      selectRow: null,
      detailDataList: [],
      detailDataItem: {
      },
      columns: [
        {
          label: '扣款项名称',
          slot: 'deductionName'
        },
        {
          label: '扣款项金额',
          slot: 'deductionAmount'
        },
        {
          label: '备注',
          slot: 'remark'
        }
      ],
      listColumns: [
        {
          label: '项目名称',
          prop: 'projName'
        },
        {
          label: '机构',
          prop: 'organization'
        },
        {
          label: '编制人',
          prop: 'founderName'
        },
        {
          label: '编制时间',
          prop: 'createTime',
          slot: 'createTime',
          width: '90'
        },
        {
          label: '承建单位',
          prop: 'contractor'
        },
        {
          label: '省份编码',
          prop: 'provinceCode'
        },
        {
          label: '省名称',
          prop: 'provinceName'
        },
        {
          label: '地市编码',
          prop: 'cityCode'
        },
        {
          label: '市名称',
          prop: 'cityName'
        },
        {
          label: '项目状态',
          prop: 'projStatus',
          slot: 'projStatus'
        },
        {
          label: '项目等级',
          prop: 'projLevel'
        },
        {
          label: '项目类型',
          prop: 'projType',
          slot: 'projType'
        }
      ],
      showTargetSetListModal: false,
      fileList: [
      ],
      rules: {
        projectName: [
          { required: true, message: '请选择项目名称', trigger: 'change' }
        ],
        month: [
          { required: true, message: '请选择结算月份', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            const source = this.data[key]
            if (typeof source === 'boolean') {
              this.form[key] = source ? 1 : 0
            } else {
              this.form[key] = source
            }
          })
        })
      } else if (!val) {
        this.fileList = []
        this.detailDataList = []
        this.$refs.form.resetFields()
      }
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    addRow() {
      this.detailDataList = [
        ...this.detailDataList,
        {
          deductionName: '',
          deductionAmount: '',
          remark: ''
        }
      ]
    },
    getSelectedRows(data) {
      if (data) {
        // this.supplierInfo = data
        const { projName } = data
        this.form.projectName = projName
      }
    },
    addDetail() {
      this.$refs.detailForm.validate((valid) => {
        if (valid) {
          const item = JSON.parse(JSON.stringify(this.detailDataItem))
          this.detailDataList.push(item)
          this.showAddDetailModal = false
          this.$refs.detailForm.resetFields()
        }
      })
    },
    cancelAddDetail() {
      this.showAddDetailModal = false
    },
    delDetailDataItem() {
      if (this.selectRow) {
        const index = this.detailDataList.indexOf(this.selectRow)
        this.detailDataList.splice(index, 1)
      }
    },
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getDocDetail(id) {
      getGeneralContractSettleInfoDetail({ generalContractSettleInfoId: id }).then(res => {
        const { lstAttachment, lstGeneralContractSettleDetail } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        if (lstGeneralContractSettleDetail && lstGeneralContractSettleDetail.length > 0) {
          this.detailDataList = lstGeneralContractSettleDetail
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    deleteDetail() {
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            generalContractSettleInfo: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstGeneralContractSettleDetail: this.detailDataList.map(item => {
              return {
                ...item,
                ...this.optType === 'edit' ? {
                  generalContractSettleInfoId: this.data.id
                } : {}
              }
            }),
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addGeneralContractSettleInfo : updateGeneralContractSettleInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
